/* weehttpd
* Copyright (c) 2008 Jeffery Myers
*
* This package is free software;  you can redistribute it and/or
* modify it under the terms of the license found in the file
* named COPYING that should have accompanied this file.
*
* THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/


#include <stdio.h>
#include <stdarg.h>
#include <sstream>
#include "timeUtils.h"
#include "textUtils.h"

#include <time.h>

#if !defined(_WIN32)
#  include <sys/time.h>
#  include <sys/types.h>
#  ifdef HAVE_SCHED_H
#    include <sched.h>
#  endif
#else /* !defined(_WIN32) */
#include "windows.h"
#endif /* !defined(_WIN32) */

double getTimerSeconds ( void )
{
#ifdef _WIN32
	return GetTickCount() * 1000.0;
#else
	struct timeval now;
	gettimeofday(&now, NULL);
	return double(now.tv_sec) + (1.0e-6 * double(now.tv_usec));
#endif // _WIN32
}

void localTimeDOW(int *year, int *month, int* day, int* dayOfWeek, int* hour, int* min, int* sec, bool* dst) // const
{
  time_t tnow = time(0);
  struct tm *now = localtime(&tnow);
  now->tm_year += 1900;
  ++now->tm_mon;

  if ( year )
    *year = now->tm_year;
  if ( month )
    *month = now->tm_mon;
  if ( day )
    *day = now->tm_mday;
  if ( hour )
    *hour = now->tm_hour;
  if ( min )
    *min = now->tm_min;
  if ( sec )
    *sec = now->tm_sec;
  if ( dst )
    *dst = now->tm_isdst != 0;
  if(dayOfWeek)
    *dayOfWeek = now->tm_wday;
}

void UTCTime(int *year, int *month, int* day, int* dayOfWeek, int* hour, int* min, int* sec, bool* dst) // const
{
  time_t tnow = time(0);
  struct tm *now = gmtime(&tnow);
  now->tm_year += 1900;
  ++now->tm_mon;

  if ( year )
    *year = now->tm_year;
  if ( month )
    *month = now->tm_mon;
  if ( day )
    *day = now->tm_mday;
  if ( hour )
    *hour = now->tm_hour;
  if ( min )
    *min = now->tm_min;
  if ( sec )
    *sec = now->tm_sec;
  if ( dst )
    *dst = now->tm_isdst != 0;
  if(dayOfWeek)
    *dayOfWeek = now->tm_wday;
}

void getLocalTime(WeeTime &ts)
{
  localTimeDOW( &ts.year, &ts.month, &ts.day, &ts.dayofweek, &ts.hour, &ts.minute, &ts.second, &ts.daylightSavings);
}

void getUTCTime ( WeeTime &ts )
{
  UTCTime(&ts.year, &ts.month, &ts.day,&ts.dayofweek, &ts.hour, &ts.minute, &ts.second, &ts.daylightSavings);
}

std::string printTime ( WeeTime &ts, const char* _timezone )
{
  std::string time;
  appendTime(time,ts,_timezone);
  return time;
}

void appendTime(std::string & text, WeeTime &ts, const char* _timezone)
{
  switch(ts.dayofweek) {
  case 1:
    text += "Mon";
    break;
  case 2:
    text += "Tue";
    break;
  case 3:
    text += "Wed";
    break;
  case 4:
    text += "Thu";
    break;
  case 5:
    text += "Fri";
    break;
  case 6:
    text += "Sat";
    break;
  case 0:
    text += "Sun";
    break;
  }

  text += format(", %d ",ts.day);

  switch(ts.month) {
  case 0:
    text += "Jan";
    break;
  case 1:
    text += "Feb";
    break;
  case 2:
    text += "Mar";
    break;
  case 3:
    text += "Apr";
    break;
  case 4:
    text += "May";
    break;
  case 5:
    text += "Jun";
    break;
  case 6:
    text += "Jul";
    break;
  case 7:
    text += "Aug";
    break;
  case 8:
    text += "Sep";
    break;
  case 9:
    text += "Oct";
    break;
  case 10:
    text += "Nov";
    break;
  case 11:
    text += "Dec";
    break;
  }

  text += format(" %d %d:%d:%d ",ts.year,ts.hour,ts.minute,ts.second);
  if (_timezone)
    text += _timezone;
  else
    text += "GMT";
}



// Local Variables: ***
// mode: C++ ***
// tab-width: 8 ***
// c-basic-offset: 2 ***
// indent-tabs-mode: t ***
// End: ***
// ex: shiftwidth=2 tabstop=8
